package com.starinside.wp.repository.impl;

import com.starinside.wp.foundation.vo.FamilyGameDetailVO;
import com.starinside.wp.foundation.vo.FamilyGameVO;
import com.starinside.wp.repository.BaseNativeSqlRepository;
import com.starinside.wp.repository.FamilyGameNativeRepository;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class FamilyGameNativeRepositoryImpl  extends BaseNativeSqlRepository implements FamilyGameNativeRepository{


    public FamilyGameDetailVO getFamilyGame(String gameId){
        String sql =  String.format("SELECT ID,Title,Media,thumb,Content,img FROM m_test_store where ID = '%s' and IsDelete=0", gameId);

        List<FamilyGameDetailVO> items = sqlObjectList(sql, FamilyGameDetailVO.class);

        if(items.size() > 0){
            return items.get(0);
        }

        return null;
    }

    public List<Integer> getFamilyGameSupportedMonth(){
        String sql =  String.format("SELECT distinct(a.classMonth) FROM m_test_storeMonth a WHERE a.Type = '2' and a.IsDelete=0");

        List<Integer> items = sqlSingleList(sql);

        return items;
    }


    public List<FamilyGameVO> getFamilyGamesByMonth(Integer month){
        String sql = "SELECT a.Title,a.ID,a.classMonth,b.Media,b.img FROM m_test_storeMonth a INNER JOIN m_test_store b ON a.ID = b.ID WHERE a.Type = '2' and a.IsDelete=0 " +
                (month == null? "": String.format("and a.classMonth=%d", month));

        List<FamilyGameVO> items = sqlObjectList(sql, FamilyGameVO.class);

        return items;
    }

    public List<HashMap<String, Object>> getAllFamilyGames(){
        String sql = "SELECT a.Title,a.ID,a.classMonth,b.Media,b.img FROM m_test_storeMonth a INNER JOIN m_test_store b ON a.ID = b.ID WHERE a.Type = '2' and a.IsDelete=0";

        List<FamilyGameVO> items = sqlObjectList(sql, FamilyGameVO.class);

        Map<Integer, List<FamilyGameVO>> map = new HashMap<Integer, List<FamilyGameVO>>();

        for (FamilyGameVO game : items ) {
            Integer key  = game.getClassMonth();
            if(map.containsKey(key)){
                List<FamilyGameVO> list = map.get(key);
                list.add(game);
            }else{
                List<FamilyGameVO> list = new ArrayList<FamilyGameVO>();
                list.add(game);
                map.put(key, list);
            }
        }


        List<Map.Entry<Integer, List<FamilyGameVO>>> ret = new ArrayList<>(map.entrySet());

        ret.sort(new Comparator<Map.Entry<Integer, List<FamilyGameVO>>>() {
            @Override
            public int compare(Map.Entry<Integer, List<FamilyGameVO>> o1, Map.Entry<Integer, List<FamilyGameVO>> o2) {
                return o1.getKey().intValue() - o2.getKey().intValue();
            }
        });

        List<HashMap<String, Object>> result = new ArrayList<>() ;

        for(Map.Entry o1 : ret){
            HashMap m = new HashMap<String, Object>();
            m.put("classMonth", o1.getKey());
            m.put("datas", o1.getValue());
            result.add(m);
        }

        return result;
    }

    @Override
    public List<FamilyGameVO> getNewMomsFood() {
        String sql = "SELECT a.Title,a.ID,a.classMonth,b.Media,b.img FROM m_test_storeMonth a INNER JOIN m_test_store b ON a.ID = b.ID WHERE a.Type = '4' AND a.classMonth='1' AND a.IsDelete=0 AND a.Media IS NOT NULL";

        List<FamilyGameVO> items = sqlObjectList(sql, FamilyGameVO.class);

        return items;
    }

    @Override
    public List<FamilyGameVO> getBabyKitchenItemsByMonth(Integer month) {
        String sql = "SELECT a.Title,a.ID,a.classMonth,b.Media,b.img FROM m_test_storeMonth a INNER JOIN m_test_store b ON a.ID = b.ID WHERE a.Type = '4' " +
                String.format("AND a.classMonth='%s' AND a.IsDelete=0", month);

        List<FamilyGameVO> items = sqlObjectList(sql, FamilyGameVO.class);

        return items;
    }

    @Override
    public List<Integer> getBabyKitchenSupportedMonth() {
        String sql =  String.format("SELECT distinct(a.classMonth) FROM m_test_storeMonth a WHERE a.Type = '4' and a.IsDelete=0 and a.classMonth > 5");

        List<Integer> items = sqlSingleList(sql);

        return items;
    }

}
